package iot.core.manager.id;

import iot.core.data.mapper.HTTbIDMapper;
import iot.core.springboot.ApplicationUtils;
import iot.core.springboot.properties.SnowflakeProperty;
import iot.core.util.MyDateTime;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class HTIDManager {

    SnowflakeIdWorker idWorker;
    HTTbIDMapper htTbIDMapper;
    private static volatile HTIDManager instance = null;
    //2.构造方法设为 private
    private HTIDManager() {
        var snowflakeProperty= ApplicationUtils.getBean(SnowflakeProperty.class);
        idWorker=new SnowflakeIdWorker(snowflakeProperty.getMachineId(),snowflakeProperty.getDataCenterId());
        htTbIDMapper=ApplicationUtils.getBean(HTTbIDMapper.class);
    }
    public static HTIDManager getInstance() {
        if (instance == null) {
            synchronized (HTIDManager.class) {
                if (instance == null) {
                    instance = new HTIDManager();
                }
            }
        }
        return instance;
    }


    public  long nextLong(){
        return idWorker.nextId();
    }
    public UUID nextGuid()
    {
        return java.util.UUID.randomUUID();
    }

    public String nextGuidStr()
    {
        return  nextGuid().toString().toLowerCase();
    }


    public Date LongIDToDate(long id)
    {
        var d = idWorker.getDateById(id);
        return d;
    }
    public   long DateToLongID(Date  dt)
    {
        return  idWorker.getIdByDate(dt);
    }


    Lock lock = new ReentrantLock();
    public   Long nextDBID(String name){
         var l=nextDBIDS(name,1);
         return l.get(0);
    }
    public List<Long> nextDBIDS(String name, int len){
        var l=new ArrayList<Long>();
        try {
            lock.lock();
            var qw=htTbIDMapper.getQueryWrapper();
            qw.eq("TName",name);
            var m=htTbIDMapper.selectOne(qw);
            if (m.isIncludeDT())
            {
                if (m.isIsDayRestart())
                {
                    if (!m.getCurrentDT().equals(MyDateTime.todayShort() ) )
                    {
                        m.setCurrentDT(MyDateTime.todayShort());
                        m.setIDV(0L);
                    }
                }
                else
                {

                }

                for (var i = 0; i < len; i++)
                {
                    m.setIDV(m.getIDV()+1);
                    var r =(long)  ( Long.parseLong(m.getCurrentDT() ) * Math.pow(10, m.getIDLength())+m.getIDV() );
                    l.add(r);
                }
                htTbIDMapper.updateById(m,false);
                return l;
            }
            else
            {

                for (var i = 0; i < len; i++)
                {
                    m.setIDV(m.getIDV()+1);
                    var r =m.getIDV();
                    l.add(r);
                }

                htTbIDMapper.updateById(m,false);
                return l;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();// 必须在finally中释放
        }
        return l;
    }
}
